package com.baosight.sczx.zm.ct.service;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.ct.SMSModel;
import com.baosight.sczx.common.zm.cInterface.ct.SystemSet;
import com.baosight.sczx.zm.ct.domain.ZMCTSms;

/**
 * <p>Title: ServiceZMCT30.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author  李红星
 * @version 1.0
 * <pre>Histroy:
 *          2012-6-13 11:02:39  李红星  Create
 *</pre>
 */
public class ServiceZMCT30 extends ServiceEPBase {
	
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCT30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2012-6-13 11:03:04 李红星
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = null;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = query(inInfo);
		return outInfo;
	}

	/**
	 * 未发短信查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCT30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 11:03:57 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
//		String fdPreparerDepId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();
//		StaticTools.setQueryParameters(inInfo, "fdPreparerDepId", fdPreparerDepId, true);//添加查询条件 操作用户
		EiInfo outInfo = super.query(inInfo, "ZMCTSms.query", new ZMCTSms());				
		return outInfo;
	}
	
	/**
	 * 短信新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCT30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 11:08:18 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
//		boolean validateValue = validate(inInfo, "ZMCTSms.validate", new HashMap<String, String>());
//		
//		if(validateValue) {
//			return inInfo;
//		}
//		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CT_SMS"));//主键
			inInfo.getBlock("result").setCell(0, "fdPreparerId", UserSession.getUserId());
			inInfo.getBlock("result").setCell(0, "fdPreparer", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(0, "fdPreparerDepId", UserSession.getInSessionProperty("orgCode"));
//			inInfo.getBlock("result").setCell(0, "fdStatus", "草拟");
			EiInfo outinfo = super.insert(inInfo, "ZMCTSms.insert", "result");
			String status=inInfo.getBlock("result").getCell(0, "fdStatus").toString();
			if(status.equals("已发送")){
				send(inInfo);
			}
			return outinfo;
//		}
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCT30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 13:53:35 李红星
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCTSms.update");
		String status=inInfo.getBlock("result").getCell(0, "fdStatus").toString();
		if(status.equals("已发送")){
			send(inInfo);
		}
		return outInfo;
		
	}
	

	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCT30</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-6-13 13:54:00 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCTSms.delete");
		EiInfo outInfo = query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		
		return outInfo;
	}
	
	/**
	 * 发送
	 * <p>Function: send</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2012-6-13 13:54:43  李红星
	 * </p>
	 *
	 */
	public EiInfo send(EiInfo inInfo) 
	{
		if("否".equals(SystemSet.getInstance().getFdStart()))//判断是否开启服务，若没有开启服务就不执行定时活动
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("服务未开启，请查看系统配置");
			return inInfo;
		}
		
        //给短信中间件表中写入记录
        for(int i=0;i<inInfo.getBlock("result").getRowCount();i++){
    		inInfo.getBlock("result").setCell(i, "fdStatus", "已发送");
    		inInfo.getBlock("result").setCell(i, "fdSendTime", StaticTools.getDateTime());

            String phone=inInfo.getBlock("result").getCell(i, "fdMobilePhone").toString();
            String content=inInfo.getBlock("result").getCell(i, "fdSmsContent").toString();
            String createtime=inInfo.getBlock("result").getCell(i, "fdCreateTime").toString();
            
            SMSModel model = new SMSModel(dao);
            model.generateSMS(content, phone, createtime);            
        }
		super.update(inInfo, "ZMCTSms.update");       
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
}
